home *** CD-ROM | disk | FTP | other *** search
- #!/usr/bin/perl -w
-
- use strict;
- use Cwd;
- use File::Find;
- use File::Copy;
- use Text::Bastardize;
- use HTML::Parser 3.05;
- use Getopt::Std;
-
- my $pathSeparator;
- my $isMacOS;
- my $scriptDir = cwd();
- BEGIN {
- if ($^O =~ /MacOS/i) {
- $pathSeparator = ":";
- $isMacOS = 1;
- } else {
- $pathSeparator = "/";
- $isMacOS = 0;
- }
- }
-
- my %options = ();
- my $modifier="rev";
- &getopts("b:", \%options);
- if ($options{b}) {
- my $bastardizationMode;
- $bastardizationMode = $options{b};
- SWITCH: { # determine which type of comment we're in
- ($bastardizationMode =~ /^rev/i) && do {$modifier = "rev"; last SWITCH;};
- ($bastardizationMode =~ /^red/i) && do {$modifier = "rdct"; last SWITCH;};
- ($bastardizationMode =~ /^pig/i) && do {$modifier = "pigIt"; last SWITCH;};
- ($bastardizationMode =~ /^cen/i) && do {$modifier = "censor"; last SWITCH;};
- print "Unknown bastardization mode--we'll reverse the text anyway.\n";
- }
- }
-
-
- ########################## Input Folder and Files #######################
- my @inputFiles;
- my $inputDir;
-
- if (($#ARGV == 0) && (-d $ARGV[0])) {
- $inputDir = $ARGV[0];
- $inputDir =~ s|(.*)/$|$1|; # get rid of trailing slash, if any
- if ($inputDir !~ /^\//) { # not absolute path -- !!! should check for ~
- $inputDir = $scriptDir.$pathSeparator.$inputDir;
- }
- &find({wanted => \&getFiles, follow => 1}, $inputDir);
- } else {
- die "You must specify a single input directory for processing.\n";
- }
- unless (@inputFiles) { print "No valid input files specified. \n\n";};
-
- sub getFiles {
- my $filePath = $File::Find::name;
- my $fileName = $_;
-
- if ($fileName =~ /\.html$/) {
- push(@inputFiles, $filePath);
- }
- }
-
-
- my $fileString;
- foreach my $file (@inputFiles) {
- $fileString='';
- my $p = HTML::Parser->new(unbroken_text => 0,
- default_h => [ \&printIt, "text" ],
- text_h => [ \&$modifier,"text" ],);
-
- $p->parse_file($file) || die "Can't open file $file: $!\n";
-
- # my @pathParts = split (/\//, $file);
- # my $fileName = pop (@pathParts);
-
- # my $filePathWithoutFilename = join("/", @pathParts);
- # my $archiveName = $fileName."~";
- # my $archive = $filePathWithoutFilename."/".$archiveName;
- if (unlink($file)) {
- open (FILE, ">$file") || die "Can't open file $file.\n";
- print FILE $fileString;
- close (FILE);
- } else {
- die("Couldn't delete $file.\n");
- }
- }
-
- sub addToFileString {
- my $addition = shift;
-
- $fileString .= $addition;
- }
-
-
- sub printIt {
- &addToFileString(@_);
- };
-
- sub rev {
- my $line = shift;
- if ($line =~ /^\s*$/) {
- &addToFileString("$line");
- return;
- } elsif (length($line)) {
- my $text = new Text::Bastardize;
- my @words = split (/\s+|\n+/, $line);
- foreach my $word (@words) {
- $word =~ s/^\s+|\s+//;
- if ($word =~ /^&/) {
- &addToFileString($word);
- } else {
- $text->charge("@{[$word]}");
- &addToFileString("@{[$text->rev()]} ");
- }
- }
- }
- &addToFileString("\n");
- };
-
- sub censor {
- my $line = shift;
- if ($line =~ /^\s*$/) {
- &addToFileString("$line");
- return;
- } elsif (length($line)) {
- my $text = new Text::Bastardize;
- my @words = split (/\s+|\n+/, $line);
- foreach my $word (@words) {
- $word =~ s/^\s+|\s+//;
- $text->charge("@{[$word]}");
- &addToFileString("@{[$text->censor()]} ");
- }
- }
- &addToFileString("\n");
- };
-
- sub rdct {
- my $line = shift;
- if ($line =~ /^\s*$/) {
- &addToFileString("$line");
- return;
- } elsif (length($line)) {
- my $text = new Text::Bastardize;
- my @words = split (/\s+|\n+/, $line);
- foreach my $word (@words) {
- $word =~ s/^\s+|\s+//;
- $text->charge("@{[$word]}");
- &addToFileString("@{[$text->rdct()]} ");
- }
- }
- &addToFileString("\n");
- };
-
- sub pigIt {
- my $line = shift;
- if ($line =~ /^\s*$/) {
- &addToFileString("$line");
- return;
- } elsif (length($line)) {
- # print "\n----------------\n$line\n=================\n\n";
- my $text = new Text::Bastardize;
- my @words = split (/\s+|\n+/, $line);
- foreach my $word (@words) {
- # print "testing==>'$word'\n";
- if (($word =~ /^([a-z]|[A-Z])+$/i) && ($word =~ /[aeiou]/)) {
- $word =~ s/^\s+|\s+//;
- # $word = lc($word);
- # print "\n---->'$word'\n";
- $text->charge("@{[$word]}");
- &addToFileString("@{[$text->pig()]} ");
- } else {
- &addToFileString("$word ");
- }
- }
- }
- &addToFileString("\n");
- };
-
-
- sub makeAbsolutePath {
- my $relPath = shift;
- my $relTo = shift;
- if ($relPath !~ /^\//) { # doesn't start with a slash
- $relPath = $relTo."/".$relPath;
- }
- return $relPath;
- }
-
-